package bishi.meituan;

import java.util.*;

public class meituan1 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNext()) {
            int total = in.nextInt();//total 商品数
            Map<String,Integer> plays = new HashMap<>();//plays 记录商品名称出现数量
            Map<String,Integer> money = new HashMap<>();//money 记录商品价格
            in.nextLine();
            while(total > 0) {

                String name = in.next();
                name = name.substring(4,name.length());
                int my = in.nextInt();

                plays.put(name,plays.getOrDefault(name,0) + 1);
                money.put(name,my);
                total--;
            }
            //开始购买
            int nums = in.nextInt();
            in.nextLine();
            while(nums > 0) {
                String goodName = in.next();
                int goodMy = in.nextInt();
                //不存在
                if(!plays.containsKey(goodName)) {
                    System.out.println("Invalid selection");
                }else {
                    //存在
                    if(plays.get(goodName) > 1) {
                        System.out.println("Error! Multiple items");
                    }else if( goodMy-money.get(goodName)  < 0) {
                        System.out.println("Insufficient funds");
                    }else {
                        System.out.println("Purchase successful, change: " + (goodMy-money.get(goodName)));
                    }
                }

                nums--;
            }


        }


    }

    /*static int reser(String demo) {
        int sum = 0;
        for(int i =0;i<demo.length();i++) {
            sum *= 10;
            char temp = demo.charAt(i);
            sum += temp-'0';

        }
        return sum;
    }*/
}
